package com.telecom.cms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dto.*;

import com.telecom.cms.util.CurrentTime;

public class SEG_TEMPLATE_ATOM_DEFDAO {
	private static final String SQL_FINDBYID = "select * from "
			+ Constant.SEG_TEMPLATE_ATOM_DEF + " where Template_Id = ? ";

	private static final String SQL_INSERT = "insert into "
			+ Constant.SEG_TEMPLATE_ATOM_DEF
			+ " (Template_Id,Biz_Atom_Id,Biz_Atom_Name,Default_Value,Default_Value_Name,Value_Type_Id,Is_Value_Not_Empty,Biz_Rule_Id,Calc_Rule,Display_Order)Values(?,?,?,?,?,?,?,?,?,?)";

	private static final String SQL_DELETE = "delete from "
			+ Constant.SEG_TEMPLATE_ATOM_DEF + " where Template_Id = ?";

	private Connection conn = null;

	public SEG_TEMPLATE_ATOM_DEFDAO(Connection con) {

		this.conn = con;

	}

	public List findById(int TemplateId) {

		PreparedStatement psmt = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			psmt = conn.prepareStatement(SQL_FINDBYID);
			psmt.setInt(1, TemplateId);
			rs = psmt.executeQuery();
			while (rs.next()) {
				SEG_TEMPLATE_ATOM_DEFDTO seg_template_atom_defdto = new SEG_TEMPLATE_ATOM_DEFDTO();
				seg_template_atom_defdto.setBizAtomId((rs
						.getInt("Biz_Atom_Id")));
				seg_template_atom_defdto.setTemplateId(rs.getInt("Template_Id"));
				seg_template_atom_defdto.setBizAtomName(rs
						.getString("Biz_Atom_Name"));
				seg_template_atom_defdto.setValueTypeId(rs.getString("Value_Type_Id"));
				seg_template_atom_defdto.setBizRuleId(rs.getInt("Biz_Rule_Id"));
				seg_template_atom_defdto.setIsValueNotEmpty(rs.getString("Biz_Rule_Id"));
				seg_template_atom_defdto.setDefaultValue(rs.getString("Default_Value"));
				seg_template_atom_defdto.setDefaultValueName(rs.getString("Default_Value_Name"));
				seg_template_atom_defdto.setDisplayOrder(rs.getInt("Display_Order"));
				list.add(seg_template_atom_defdto);
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {

					rs.close();
				}
				if (psmt != null) {
					psmt.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	public void insert(SEG_TEMPLATE_ATOM_DEFDTO templateAtom) {

		PreparedStatement psmt = null;
		try {
			psmt = conn.prepareStatement(SQL_INSERT);
			psmt.setInt(1, templateAtom.getTemplateId());
			psmt.setInt(2, templateAtom.getBizAtomId());
			psmt.setString(3, templateAtom.getBizAtomName());
			psmt.setString(4, templateAtom.getDefaultValue());
			psmt.setString(5, templateAtom.getDefaultValueName());
			psmt.setString(6, templateAtom.getValueTypeId());
			psmt.setString(7, templateAtom.getIsValueNotEmpty());
			psmt.setInt(8, templateAtom.getBizRuleId());
			psmt.setString(9, templateAtom.getCalcRule());
			psmt.setInt(10, templateAtom.getDisplayOrder());

			psmt.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psmt != null) {
					psmt.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void delete(int templateId) {

		PreparedStatement psmt = null;
		try {
			psmt = conn.prepareStatement(SQL_DELETE);
			psmt.setInt(1, templateId);
			psmt.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {

				if (psmt != null) {
					psmt.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}